package com.cloud.report.api.web;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cloud.report.api.entity.ReportDatasource;
import com.cloud.report.api.service.ReportDataSourceService;
import com.cloud.report.common.result.Quokka;

@RestController
@RequestMapping("/report/api/datasource")
public class ReportDataSourceController {
	
	@Autowired
	private ReportDataSourceService reportDataSourceService;

	@GetMapping("list")
	public Quokka listDataSource() {
		return new Quokka(true, reportDataSourceService.listDataSource());
	}

	@GetMapping("detail")
	public Quokka showDetail(int id){
		return new Quokka(true, reportDataSourceService.load(id));
	}
	
	@PostMapping("saveupdate")
	public Quokka saveUpdateDataSource(@RequestBody ReportDatasource ds) {
		return new Quokka(true, reportDataSourceService.saveUpdateDataSource(ds));
	}

	@GetMapping("delete")
	public Quokka deleteDataSource(long id) {
		return new Quokka(true, reportDataSourceService.deleteDataSource(id));
	}

	@PostMapping("tryconn")
	public Quokka tryConn(@RequestBody ReportDatasource ds){
		String message = reportDataSourceService.tryConnection(ds);
		Quokka qk = new Quokka();
		if(StringUtils.isEmpty(message)){
			qk.setOk(true);
		}else{
			qk.setOk(false);
			qk.setMessage(message);
		}
		return qk;
	}
	
	@GetMapping("test")
	public Quokka test(){
		reportDataSourceService.test("BBB");
		return new Quokka(true);
	}
}
